package com.csq.androidproject.views.calendarview.caldroid.adapters;

import hirondelle.date4j.DateTime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.csq.androidproject.R;
import com.csq.androidproject.utils.DateUtils;
import com.csq.androidproject.views.calendarview.caldroid.SpecialCalendarGridCell;
import com.csq.androidproject.views.calendarview.caldroid.views.CalendarView;

/**
 * The CaldroidGridAdapter provides customized view for the dates gridview
 */
public class CaldroidGridAdapter extends BaseAdapter {
    
    protected Context context;
    protected Resources resources;
    
    /**
     * 当前年
     */
	protected int year;
	/**
	 * 当前月
	 */
	protected int month;
	
	/**
	 * 所有日期
	 */
	protected List<DateTime> datetimeList = new ArrayList<DateTime>(1);
	/**
	 * 特殊日期
	 */
	protected HashMap<String, SpecialCalendarGridCell> specialDates;
	/**
	 * 今天日期
	 */
	protected DateTime today;

	
	
    public CaldroidGridAdapter(Context context) {
        super();
        this.context = context;
        this.resources = context.getResources();
    }
    
    public void setAdapterDateTime(int year, int month, int startDayOfWeek) {
        this.month = month;
        this.year = year;
        this.datetimeList = DateUtils.getFullWeeks(this.month, this.year,startDayOfWeek);
        notifyDataSetChanged();
    }

	public void setAdapterDateTime(DateTime dateTime, int startDayOfWeek) {
		this.month = dateTime.getMonth();
		this.year = dateTime.getYear();
		this.datetimeList = DateUtils.getFullWeeks(this.month, this.year,startDayOfWeek);
		notifyDataSetChanged();
	}
	
	public void updateSpecialDates(HashMap<String, SpecialCalendarGridCell> specialDates){
	    this.specialDates = specialDates;
        notifyDataSetChanged();
	}
	
	public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public List<DateTime> getDatetimeList() {
        return datetimeList;
    }
	
	protected DateTime getToday() {
        if (today == null) {
            today = DateUtils.getCurrentDataTime();
        }
        return today;
    }
	
	@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.datetimeList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return datetimeList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView cellView = (TextView) convertView;

        // For reuse
        if (convertView == null) {
            cellView = (TextView) inflater.inflate(R.layout.calendar_date_cell, null);
        }

        customizeTextView(position, cellView);

        return cellView;
    }
    
    protected void customizeTextView(int position, TextView cellView) {
        
        DateTime dateTime = this.datetimeList.get(position);
        String dateTimeStr = DateUtils.getYMDString(dateTime);
        
        if(dateTime.getMonth() != month){
            //灰色
            cellView.setBackgroundResource(CalendarView.disableBackgroundDrawableResId);
            cellView.setTextColor(CalendarView.disabledTextColor);
        }else{
            if(specialDates != null && specialDates.containsKey(dateTimeStr)){
                //特殊
                SpecialCalendarGridCell sp = specialDates.get(dateTimeStr);
                cellView.setBackgroundResource(sp.backgroundDrawableResId);
                cellView.setTextColor(sp.textColor);
            }else{
                if(DateUtils.getYMDString(getToday()).equals(dateTimeStr)){
                    //今天
                    cellView.setBackgroundResource(CalendarView.todayBackgroundDrawableResId);
                    cellView.setTextColor(CalendarView.todayTextColor);
                }else{
                    //常规
                    cellView.setBackgroundResource(CalendarView.normalBackgroundDrawableResId);
                    cellView.setTextColor(CalendarView.normalTextColor);
                }
            }
        }
        
        cellView.setText("" + dateTime.getDay());
    }
	
}
